package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.makerspace.dao.CompetitionDAO;
import com.example.makerspace.dao.CompetitionParentDAO;
import com.example.makerspace.dao.NoticeDAO;
import com.example.makerspace.domain.CompetitionParentDO;
import com.example.makerspace.domain.NoticeDO;
import com.example.makerspace.dto.NoticeDTO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Result;
import com.example.makerspace.mapper.CompetitionParentMapper;
import com.example.makerspace.service.NoticeService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class NoticeServiceImpI implements NoticeService {

    @Resource
    private NoticeDAO  noticeDAO;


    @Resource
    private CompetitionParentDAO competitionParentDAO;

    @Override
    public Result addNotice(NoticeDTO noticeDTO) {
        NoticeDO noticeDO = new NoticeDO();
        BeanUtils.copyProperties(noticeDTO,noticeDO);
        if(noticeDTO.getCompetitionParentId()!=-1){
            CompetitionParentDO competitionParentDO = competitionParentDAO.selectById(noticeDTO.getCompetitionParentId());
            noticeDO.setCompetitionTitle(competitionParentDO.getTitle());
        }
        boolean insert = noticeDAO.insert(noticeDO);
        if(insert){
            return Result.success("增加公告成功");
        }else {
            return Result.success("增加公告失败");
        }
    }
    @Override
    public Result deleteNotice(Integer id) {
        boolean b = noticeDAO.deleteById(id);
        if(b){
            return Result.success("删除公告成功");
        }
        return Result.success("删除公告失败");
    }

    @Override
    public Result selectNotices(PageRequest pageRequest) {
        IPage<NoticeDO> noticeDOIPage;
        Map<String, Object> params = pageRequest.getParams();
        String title = MapUtils.getString(params, "title");
        String result = MapUtils.getString(params, "competitionParentId");
        int competitionParentId = Integer.parseInt(result);
        if(title.equals("")){
            noticeDOIPage= noticeDAO.selectByPage(pageRequest.getPagination(),competitionParentId);
            return Result.success(noticeDOIPage);
        }
           noticeDOIPage = noticeDAO.selectByLike(pageRequest.getPagination(),title,competitionParentId);
        return Result.success(noticeDOIPage);
    }

    @Override
    public Result bindNotices(NoticeDTO noticeDTO) {
        NoticeDO noticeDO = new NoticeDO();
        Integer competitionParentId = noticeDTO.getCompetitionParentId();
        CompetitionParentDO competitionParentDO = competitionParentDAO.selectById(competitionParentId);
        BeanUtils.copyProperties(noticeDTO,noticeDO);
        noticeDO.setCompetitionTitle(competitionParentDO.getTitle());
        boolean b = noticeDAO.updateById(noticeDO);
        if(b){
            return Result.success("绑定公告成功");
        }
        return Result.success("绑定公告失败");
    }

    @Override
    public Result selectAllNotices(PageRequest pageRequest) {
        IPage<NoticeDO> noticeDOIPage;
        Map<String, Object> params = pageRequest.getParams();
        String title = MapUtils.getString(params, "title");
        if(title.equals("")){
            noticeDOIPage= noticeDAO.selectByPage2(pageRequest.getPagination());
            return Result.success(noticeDOIPage);
        }
        noticeDOIPage = noticeDAO.selectByLike2(pageRequest.getPagination(),title);
        return Result.success(noticeDOIPage);
    }
}
